package com.wortox.getPrice;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;

public class SumPrice3 implements SumPrice{

    @Override
    public Integer getPrice(Map<String, Integer> fruitMap) {
        SumPrice1 sumPrice1 = new SumPrice1();

        //动态代理
        SumPrice proxy = (SumPrice) Proxy.newProxyInstance(sumPrice1.getClass().getClassLoader(), sumPrice1.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Integer price = (Integer)method.invoke(sumPrice1, args);
                if(price > 100) {
                    Integer discount = price / 100 *10;
                    price = price - discount;
                }

                return price;
            }
        });

        return proxy.getPrice(fruitMap);
    }
}
